package com.intel.two.controller.older;

import com.intel.two.common.R;
import com.intel.two.common.older.LayuiPageVo;
import com.intel.two.pojo.older.Activity;
import com.intel.two.service.older.ActivityService;
import com.intel.two.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @RequestMapping("/activitylist")
    public LayuiPageVo<Activity> activityPageData(@RequestParam Map<String, Object> params) {
        return activityService.activityPageDate(params);
    }

    @RequestMapping("/add")
    public R addActivity(Activity activity) {
        int result = activityService.addActivity(activity);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }

    }

    @RequestMapping("/delete")
    public R deleteById(Integer id) {
        int result = activityService.deleteById(id);
        if (result == 1) {
            return R.ok();
        } else
            return R.error();
    }

    @GetMapping("/deleteAll")
    public R deleteAll(@RequestParam Map<String, Object> map) {
        int result = activityService.deleteAll(map);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/update")
    public R updateActivity(Activity activity) {
        int result = activityService.udateActivity(activity);

        return result == 1 ? R.ok() : R.error();

    }

    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws Exception {
        //得到文件的名字
        String originalFilename = file.getOriginalFilename();

        String fileUrl = OssUtil.updateLoad(file.getInputStream(), originalFilename);
        // 上传成功之后的文件地址 写给前端

        return R.ok().data("url", fileUrl);

    }
}

